package com.company.test18;

import javafx.scene.control.Cell;

import javax.swing.*;
import javax.swing.border.LineBorder;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

/**
 * Created by Administrator on 2016.9.19.
 */
public class Test18_10 extends JApplet {
    private char whoseTurn='X';

    private Cell[][] cells=new Cell[3][3];

    private JLabel jlb=new JLabel("X's turn to play");

    public boolean isWon(char token){
        for (int i=0;i<3;i++){
            if(cells[i][0].getToken()==token
                    &&cells[i][1].getToken()==token
                    &&cells[i][2].getToken()==token){
                return true;
            }
        }

        for(int j=0;j<3;j++){
            if(cells[0][j].getToken()==token
                    &&cells[1][j].getToken()==token
                    &&cells[2][j].getToken()==token){
                return true;
            }
        }

        if(cells[0][0].getToken()==token
                &&cells[1][1].getToken()==token
                &&cells[2][2].getToken()==token){
            return true;
        }

        if(cells[2][0].getToken()==token
                &&cells[1][1].getToken()==token
                &&cells[0][2].getToken()==token){
            return true;
        }

        return false;
    }

    public boolean isFull(){

        for(int i=0;i<cells.length;i++){
            for (int j=0;j<cells[i].length;j++){
                if(cells[i][j].getToken()==' '){
                    return false;
                }
            }
        }
        return true;
    }

    public class Cell extends JPanel{
        private char token=' ';

        public void setToken(char token){
            this.token=token;
        }

        public char getToken(){
            return token;
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);

            if(token=='X'){
                g.drawLine(10,10,getWidth()-10,getHeight()-10);
                g.drawLine(getWidth()-10,10,10,getHeight()-10);
            }else if (token=='0'){
                g.drawOval(10,10,getWidth()-20,getHeight()-20);
            }
        }

        public Cell(){
            setBorder(new LineBorder(Color.black,1));
            addMouseListener(new MyMouseListener());
        }

        private class MyMouseListener extends MouseAdapter{
            @Override
            public void mousePressed(MouseEvent e) {
                super.mousePressed(e);

                if(token==' '&&whoseTurn!=' ') {
                    setToken(whoseTurn);
                    Cell.this.repaint();

                    if(isWon(whoseTurn)){
                        jlb.setText(whoseTurn+"won! The game is over");
                        whoseTurn=' ';
                    }else if(isFull()){
                        jlb.setText("Draw! The game is over");
                        whoseTurn=' ';
                    }else {
                        whoseTurn=(whoseTurn=='X')? '0':'X';

                        jlb.setText(whoseTurn+"'s turn");
                    }
                }
            }
        }
    }

    public Test18_10(){
        JPanel p=new JPanel(new GridLayout(3,3,0,0));
        for(int i=0;i<cells.length;i++){
            for (int j=0;j<cells[i].length;j++){
                cells[i][j]=new Cell();
                p.add(cells[i][j]);
            }
        }
        p.setBorder(new LineBorder(Color.red,1));
        jlb.setBorder(new LineBorder(Color.yellow,1));

        add(p,BorderLayout.CENTER);
        add(jlb,BorderLayout.SOUTH);
    }


    public static void main(String args[]){
        JFrame frame=new JFrame();
        frame.add(new Test18_10());

        frame.setSize(300,300);
        frame.setTitle("XO棋");
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
}